const dataBase = require('../../mysql')

const sql = require('../../mysql/sql')
const verify = require('../../auth/verify')
const verifyRequestMethod = require('../../tool/verifyRequestMethod')

/**
 * @description 获取个人资料
 */
module.exports = function () {
    const that = this
    const authorization = this.request.headers.authorization
    /**
     * @description 请求验证
     */
    verifyRequestMethod(this.request, 'GET').then(function () {
        /**
         * @description 授权验证
         */
        if (that.isDef(authorization)) {
            return verify(authorization)
        } else {
            return Promise.reject(new Error('缺少Authorization！'))
        }
    }).then(function (data) {
        return dataBase.connect(sql.SEL_SELECT_USER_ONE(data.username))
    }).then(function (result) {
        const map = {}
        const data = result[0]
        Object.keys(data).forEach(function (key) {
            if (key !== 'password') {
                map[key] = data[key]
            }
        })
        that.response.outPrintData({
            info: map
        }, '查询成功！')
    }).catch(function (e) {
        that.response.outPrintError((e && e.message) || '未知错误！')
    })
}